{% extends "admin/base.html" %}

{% block title %}仪表盘{% endblock %}
{% block page_title %}仪表盘{% endblock %}

{% block content %}
<!-- Stats Cards -->
<div class="row mb-4">
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-primary shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                            总文章数
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.total_posts }}</div>
                    </div>
                    <div class="col-auto">
                        <i class="fas fa-file-alt fa-2x text-gray-300"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-success shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                            已发布文章
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.published_posts }}</div>
                    </div>
                    <div class="col-auto">
                        <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-info shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                            总评论数
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.total_comments }}</div>
                    </div>
                    <div class="col-auto">
                        <i class="fas fa-comments fa-2x text-gray-300"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-warning shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                            待审核评论
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.pending_comments }}</div>
                    </div>
                    <div class="col-auto">
                        <i class="fas fa-clock fa-2x text-gray-300"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- Recent Posts -->
    <div class="col-lg-6 mb-4">
        <div class="card shadow">
            <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                <h6 class="m-0 font-weight-bold text-primary">最新文章</h6>
                <a href="{{ url_for('admin.posts') }}" class="btn btn-sm btn-primary">查看全部</a>
            </div>
            <div class="card-body">
                {% if recent_posts %}
                    {% for post in recent_posts %}
                    <div class="d-flex align-items-center mb-3">
                        <div class="flex-grow-1">
                            <h6 class="mb-1">
                                {% if post.is_published %}
                                <a href="{{ url_for('main.post_detail', slug=post.slug) }}" 
                                   class="text-decoration-none" target="_blank">
                                    {{ post.title }}
                                </a>
                                {% else %}
                                <a href="{{ url_for('main.preview_post', slug=post.slug) }}" 
                                   class="text-decoration-none" target="_blank">
                                    {{ post.title }}
                                </a>
                                {% endif %}
                            </h6>
                            <small class="text-muted">
                                {{ post.created_at.strftime('%Y-%m-%d %H:%M') }}
                                {% if post.is_published %}
                                <span class="badge bg-success ms-2">已发布</span>
                                {% else %}
                                <span class="badge bg-secondary ms-2">草稿</span>
                                {% endif %}
                            </small>
                        </div>
                        <div class="dropdown">
                            <button class="btn btn-sm btn-outline-secondary dropdown-toggle" 
                                    type="button" data-bs-toggle="dropdown">
                                操作
                            </button>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" 
                                       href="{{ url_for('admin.edit_post', id=post.id) }}">编辑</a></li>
                                {% if post.is_published %}
                                <li><a class="dropdown-item" 
                                       href="{{ url_for('main.post_detail', slug=post.slug) }}" 
                                       target="_blank">查看</a></li>
                                {% else %}
                                <li><a class="dropdown-item" 
                                       href="{{ url_for('main.preview_post', slug=post.slug) }}" 
                                       target="_blank">预览</a></li>
                                {% endif %}
                            </ul>
                        </div>
                    </div>
                    {% if not loop.last %}<hr>{% endif %}
                    {% endfor %}
                {% else %}
                <div class="text-center text-muted py-3">
                    <i class="fas fa-file-alt fa-2x mb-2"></i>
                    <p>还没有文章</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Recent Comments -->
    <div class="col-lg-6 mb-4">
        <div class="card shadow">
            <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                <h6 class="m-0 font-weight-bold text-primary">最新评论</h6>
                <a href="{{ url_for('admin.comments') }}" class="btn btn-sm btn-primary">查看全部</a>
            </div>
            <div class="card-body">
                {% if recent_comments %}
                    {% for comment in recent_comments %}
                    <div class="d-flex align-items-start mb-3">
                        <div class="flex-grow-1">
                            <div class="d-flex justify-content-between align-items-start mb-1">
                                <strong>
                                    {% if comment.guest_name %}
                                        {{ comment.guest_name }}
                                    {% elif comment.author %}
                                        {{ comment.author.username }}
                                    {% else %}
                                        匿名用户
                                    {% endif %}
                                </strong>
                                <small class="text-muted">
                                    {{ comment.created_at.strftime('%m-%d %H:%M') }}
                                </small>
                            </div>
                            <p class="mb-1 small">{{ comment.content[:100] }}{% if comment.content|length > 100 %}...{% endif %}</p>
                            <small class="text-muted">
                                文章：<a href="{{ url_for('main.post_detail', slug=comment.post.slug) }}" 
                                       class="text-decoration-none" target="_blank">
                                    {{ comment.post.title }}
                                </a>
                            </small>
                        </div>
                        <div class="dropdown">
                            <button class="btn btn-sm btn-outline-secondary dropdown-toggle" 
                                    type="button" data-bs-toggle="dropdown">
                                操作
                            </button>
                            <ul class="dropdown-menu">
                                {% if not comment.is_approved %}
                                <li><a class="dropdown-item" href="#" 
                                       onclick="approveComment({{ comment.id }})">审核通过</a></li>
                                {% endif %}
                                <li><a class="dropdown-item" href="#" 
                                       onclick="spamComment({{ comment.id }})">标记垃圾</a></li>
                                <li><a class="dropdown-item text-danger" href="#" 
                                       onclick="deleteComment({{ comment.id }})">删除</a></li>
                            </ul>
                        </div>
                    </div>
                    {% if not loop.last %}<hr>{% endif %}
                    {% endfor %}
                {% else %}
                <div class="text-center text-muted py-3">
                    <i class="fas fa-comments fa-2x mb-2"></i>
                    <p>还没有评论</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- Quick Actions -->
<div class="row">
    <div class="col-12">
        <div class="card shadow">
            <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-primary">快速操作</h6>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3 mb-3">
                        <a href="{{ url_for('admin.new_post') }}" class="btn btn-primary btn-block w-100">
                            <i class="fas fa-plus"></i> 写新文章
                        </a>
                    </div>
                    <div class="col-md-3 mb-3">
                        <a href="{{ url_for('admin.categories') }}" class="btn btn-success btn-block w-100">
                            <i class="fas fa-folder-plus"></i> 管理分类
                        </a>
                    </div>
                    <div class="col-md-3 mb-3">
                        <a href="{{ url_for('admin.tags') }}" class="btn btn-info btn-block w-100">
                            <i class="fas fa-tag"></i> 管理标签
                        </a>
                    </div>
                    <div class="col-md-3 mb-3">
                        <a href="{{ url_for('admin.comments') }}" class="btn btn-warning btn-block w-100">
                            <i class="fas fa-comment-dots"></i> 审核评论
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function approveComment(commentId) {
    if (confirm('确定要审核通过这条评论吗？')) {
        fetch(`/admin/comments/${commentId}/approve`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert(data.message);
            }
        });
    }
}

function spamComment(commentId) {
    if (confirm('确定要将此评论标记为垃圾吗？')) {
        fetch(`/admin/comments/${commentId}/spam`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert(data.message);
            }
        });
    }
}

function deleteComment(commentId) {
    if (confirm('确定要删除这条评论吗？此操作不可恢复！')) {
        fetch(`/admin/comments/${commentId}/delete`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert(data.message);
            }
        });
    }
}
</script>
{% endblock %}

